package com.syq.play.ui;

import java.util.List;

import com.syq.play.AppContent;
import com.syq.play.R;
import com.syq.play.adapter.FolderAdapter;
import com.syq.play.adapter.FolderGridAdapter;
import com.syq.play.db.entity.SongEntity;
import com.syq.play.db.entity.SongManager;
import com.syq.play.model.FolderModel;
import com.syq.play.util.BitmapUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FolderActivity extends Activity implements OnClickListener,
		OnItemClickListener {
	private static final String TAG = "FolderActivity";
	/** 艺术家 */
	public static final int TYPE_ARTIST = 0;
	/** 专辑 */
	public static final int TYPE_ALBUM = 1;
	/** 文件夹 */
	public static final int TYPE_FOLDER = 2;

	private Context context;
	private LinearLayout llBack;
	private TextView tvTitle;
	private GridView gridView;
	private FolderAdapter folderAdapter;
	private String title;
	private int type = -1;
	private BitmapUtil bitmapUtil=new BitmapUtil();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_home_page);
		initData();
		initUi();
		initListener();
		getData();
	}

	private void initData() {
		context = this;
		Intent intent = getIntent();
		Bundle data = intent.getExtras();
		if (data != null && !data.isEmpty()) {
			title = data.getString(IndexActivity.KEY_TITLE);
			type = data.getInt(IndexActivity.KEY_TYPE);
		}
	}

	private void initUi() {
		llBack = (LinearLayout) findViewById(R.id.ll_left);
		tvTitle = (TextView) findViewById(R.id.tv_title);
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setNumColumns(2);
		tvTitle.setText(title);
	}

	private void initListener() {
		llBack.setOnClickListener(this);
		gridView.setOnItemClickListener(this);
		gridView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					updateBitmap();
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
	}

	private void getData() {
		new AsyncTask<Void, Void, List<FolderModel>>() {

			@Override
			protected List<FolderModel> doInBackground(Void... params) {
				List<FolderModel> result = null;
				switch (type) {
				case TYPE_ARTIST:
					result = AppContent.mFolder.getAllArtist();
					break;
				case TYPE_ALBUM:
					result = AppContent.mFolder.getAllAlbum();
					break;
				case TYPE_FOLDER:
					result = AppContent.mFolder.getAllFolder();
					break;
				}
				return result;
			}

			protected void onPostExecute(List<FolderModel> result) {
				if (result != null && !result.isEmpty()) {
					if (folderAdapter == null) {
						folderAdapter = new FolderGridAdapter(context, result);
						gridView.setAdapter(folderAdapter);
					} else {
						folderAdapter.updateListData(result);
					}
					updateBitmap();
				}
			};
		}.execute();
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		FolderModel item=folderAdapter.getListData().get(position);
		Bundle data=new Bundle();
		data.putString(IndexActivity.KEY_TITLE, item.getFolderName());
		switch (type) {
		case TYPE_ARTIST:
			data.putInt(IndexActivity.KEY_TYPE, MediaActivity.TYPE_ARTIST);
			SRPlayActivity.mActivityGroup.mStartActivity(MediaActivity.class, data);
			break;
		case TYPE_ALBUM:
			data.putInt(IndexActivity.KEY_TYPE, MediaActivity.TYPE_ALBUM);
			SRPlayActivity.mActivityGroup.mStartActivity(MediaActivity.class, data);
			break;
		case TYPE_FOLDER:
			data.putInt(IndexActivity.KEY_TYPE, MediaActivity.TYPE_FOLDER);
			SRPlayActivity.mActivityGroup.mStartActivity(MediaActivity.class, data);
			break;
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_left:
			goBack();
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 返回方法
	 */
	private void goBack() {
		SRPlayActivity.mActivityGroup.mStartActivity(IndexActivity.class, null);
	}

	public void updateBitmap() {
		if (folderAdapter != null) {
			String selection = "";
			switch (type) {
			case TYPE_ARTIST:
				selection = SongEntity.ARTIST;
				break;
			case TYPE_ALBUM:
				selection = SongEntity.ALBUM;
				break;
			case TYPE_FOLDER:
				selection = SongEntity.FOLDER;
				break;
			}
			bitmapUtil.updateAdapterBitmap(gridView, folderAdapter, selection);
		}
	}
}
